package com.fs.uc.base;

import com.fs.uc.base.domain.FunctionalModule;
import com.fs.uc.base.domain.Permission;
import com.fs.uc.base.domain.Role;
import com.fs.uc.base.domain.RoleType;
import com.fs.uc.base.repo.ModuleRepository;
import com.fs.uc.base.repo.PermissionRepository;
import com.fs.uc.base.repo.RoleRepository;
import io.quarkus.runtime.Startup;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import javax.transaction.Transactional;

/**
 * @author 崔超
 * 2021/12/8
 */
@Singleton
@Startup
class DatabaseInit {

    final RoleRepository roleRepo;
    final ModuleRepository moduleRepo;
    final PermissionRepository permRepo;

    public DatabaseInit(RoleRepository roleRepo, ModuleRepository moduleRepo, PermissionRepository permRepo) {
        this.roleRepo = roleRepo;
        this.moduleRepo = moduleRepo;
        this.permRepo = permRepo;
    }


    @PostConstruct
    void initRoleAndPermission() {
        initUserCenter();
        initBasicInfo();
        initGatePass();
        initMeetingRoomManage();
        initWorkOrder();
        initEnergyDevice();
        initBillManage();
        initAssetsManage();
        initPatrolManage();
        initCarYardManage();
    }

    @Transactional
    void initUserCenter() {
        String moduleName = "UserCenter";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "用户中心", 0, true);
            moduleRepo.persist(module);

            Role manager = new Role("user_manager", "用户管理", RoleType.ParkOriented, module);
            Role client = new Role("basic_user", "用户鉴权基础角色", RoleType.CommonUser, module);
            roleRepo.persist(manager, client);

            Permission managerPerm = new Permission("*", "/auth/admin/", manager);
            Permission clientPerm = new Permission("*", "/auth/user/", client);
            permRepo.persist(managerPerm, clientPerm);
        }
    }

    @Transactional
    void initBasicInfo() {
        String moduleName = "BasicInfo";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "基础信息", 0, true);
            moduleRepo.persist(module);

            Role manager = new Role("basicinfo_manager", "基础信息模块管理员", RoleType.ParkOriented, module);
            Role client = new Role("basicinfo_client", "基础信息模块普通人员", RoleType.CommonUser, module);
            Role boss = new Role("enterprise_boss", "基础信息模块企业负责人", RoleType.EnterpriseOriented, module);
            roleRepo.persist(manager, client, boss);

            Permission clientPerm = new Permission("*", "/basic/v2/", client);
            Permission bossPerm = new Permission("*", "/basic/v4/", boss);
            Permission managerPerm = new Permission("*", "/basic/v1/", manager);
            permRepo.persist(clientPerm, managerPerm, bossPerm);
        }
    }

    @Transactional
    void initGatePass() {
        String moduleName = "GatePass";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "人员通行", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("pass_manager", "人员通行模块后台管理员", RoleType.ParkOriented, module);
            Role client = new Role("pass_client", "人员通行模块普通人员", RoleType.CommonUser, module);
            Role visitor = new Role("pass_visitor", "人员通行访客", RoleType.RegisteredUserOriented, module);
            roleRepo.persist(manager, client, visitor);

            Permission managerPerm = new Permission("*", "/pass/v1/", manager);
            Permission clientPerm = new Permission("*", "/pass/v2/", client);
            Permission visitorPerm = new Permission("*", "/pass/v3/", visitor);
            permRepo.persist(managerPerm, clientPerm, visitorPerm);
        }
    }

    @Transactional
    void initMeetingRoomManage() {
        String moduleName = "MeetingRoomManage";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "会议预约", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("meeting_manager", "会议预约模块后台管理员", RoleType.ParkOriented, module);
            Role client = new Role("meeting_client", "会议预约模块普通人员", RoleType.CommonUser, module);
            roleRepo.persist(manager, client);

            Permission managerPerm = new Permission("*", "/mrm/api/v1/", manager);
            Permission clientPerm = new Permission("*", "/mrm/api/v2/", client);
            permRepo.persist(managerPerm, clientPerm);
        }
    }

    @Transactional
    void initWorkOrder() {
        String moduleName = "WorkOrder";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "工单管理", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("work_manager", "工单管理模块后台管理员", RoleType.ParkOriented, module);
            Role client = new Role("work_client", "工单管理模块普通人员", RoleType.CommonUser, module);
            Role repairMan = new Role("work_worker", "工单管理模块维修员", RoleType.Additional, module);
            roleRepo.persist(manager, client, repairMan);

            Permission managerPerm = new Permission("*", "/workOrder/v1/", manager);
            Permission clientPerm = new Permission("*", "/workOrder/v2/", client);
            Permission repairPerm = new Permission("*", "/workOrder/v3/", repairMan);
            permRepo.persist(managerPerm, clientPerm, repairPerm);
        }
    }

    @Transactional
    void initEnergyDevice() {
        String moduleName = "EnergyDevice";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "能耗设备", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("energydevice_manager", "能耗设备模块后台管理员", RoleType.ParkOriented, module);
            Role meterReader = new Role("energydevice_worker", "能耗设备模块抄表员", RoleType.Additional, module);
            roleRepo.persist(manager, meterReader);

            Permission managerPerm = new Permission("*", "/energy/v1/", manager);
            Permission meterReaderPerm = new Permission("*", "/energy/v2/", meterReader);
            permRepo.persist(managerPerm, meterReaderPerm);
        }
    }

    @Transactional
    void initBillManage() {
        String moduleName = "BillManage";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "账单管理", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("bill_manager", "账单管理模块后台管理员", RoleType.ParkOriented, module);
            Role client = new Role("bill_client", "账单管理模块企业人员", RoleType.CommonUser, module);
            Role boss = new Role("bill_boss", "账单管理模块企业负责人", RoleType.EnterpriseOriented, module);
            roleRepo.persist(manager, client, boss);

            Permission managerPerm = new Permission("*", "/bill/v1/", manager);
            Permission clientPerm = new Permission("*", "/bill/v2/", client);
            Permission bossPerm = new Permission("*", "/bill/v3/", boss);
            permRepo.persist(managerPerm, clientPerm, bossPerm);
        }
    }

    @Transactional
    void initAssetsManage() {
        String moduleName = "AssetsManage";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "资产管理", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("assets_manager", "资产管理模块后台管理员", RoleType.ParkOriented, module);
            Role outWorker = new Role("assets_out_worker", "资产管理模块出库人", RoleType.Additional, module);
            Role inWorker = new Role("assets_in_worker", "资产管理模块入库人", RoleType.Additional, module);
            Role checkerWorker = new Role("assets_check_worker", "资产管理模块盘点人", RoleType.Additional, module);
            roleRepo.persist(manager, outWorker, inWorker, checkerWorker);

            Permission managerPerm = new Permission("*", "/assets/v1/", manager);
            Permission outWorkerPerm = new Permission("*", "/assets/v2/", outWorker);
            Permission inWorkerPerm = new Permission("*", "/assets/v3/", inWorker);
            Permission checkerWorkerPerm = new Permission("*", "/assets/v4/", checkerWorker);
            permRepo.persist(managerPerm, outWorkerPerm, inWorkerPerm, checkerWorkerPerm);
        }
    }

    @Transactional
    void initPatrolManage() {
        String moduleName = "PatrolManage";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "巡检管理", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("patrol_manager", "巡检管理模块后台管理员", RoleType.ParkOriented, module);
            Role patrolMan = new Role("patrol_worker", "巡检管理模块巡检员", RoleType.Additional, module);
            roleRepo.persist(manager, patrolMan);

            Permission managerPerm = new Permission("*", "/patrol/v1/", manager);
            Permission patrolManPerm = new Permission("*", "/patrol/v2/", patrolMan);
            permRepo.persist(managerPerm, patrolManPerm);
        }
    }

    @Transactional
    void initCarYardManage() {
        String moduleName = "CarYard";
        FunctionalModule module = moduleRepo.findByName(moduleName);
        if (module == null) {
            module = new FunctionalModule(moduleName, "车场管理", 0, false);
            moduleRepo.persist(module);

            Role manager = new Role("caryard_manager", "车场管理模块管理员", RoleType.ParkOriented, module);
            Role carOwner = new Role("caryard_carOwner", "车场管理车主", RoleType.RegisteredUserOriented, module);
            roleRepo.persist(manager, carOwner);

            Permission managerPerm = new Permission("*", "/yard/v1/", manager);
            Permission carOwnerPerm = new Permission("*", "/yard/v2/", carOwner);
            permRepo.persist(managerPerm, carOwnerPerm);
        }
    }
}
